package com.hmkj.admin.controller.rank;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.common.utils.ValidateUtils;
import com.hmkj.core.eolinker.endpoints.QuotationEndpoint;
import com.hmkj.core.eolinker.models.ModelRealTime;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.strategy.StrategyRank;
import com.hmkj.core.po.trader.Trader;
import com.hmkj.core.service.strategy.StrategyRankService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class RankJsonController extends GenericController<Integer, Trader> {
    @Resource
    private StrategyRankService strategyRankService;

    /**
     * 策略排行
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("strategyRank/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return strategyRankService.getDtGridList(dtGridPager, StrategyRank.class);
    }

    /**
     * 新增
     *
     * @param strategyRank
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("strategyRank/json/add")
    @RequiresPermissions("sys:rank:save")
    public ResultEntity addJson(StrategyRank strategyRank) throws Exception {
        checkModel(strategyRank);
        if (strategyRankService.selectCount(new StrategyRank(t -> {
            t.setStockCode(strategyRank.getStockCode());
            t.setDelFlag(StrategyRank.DELFLAG.NORMAL.code);
        })) > 0 || strategyRankService.selectCount(new StrategyRank(t -> {
            t.setStockName(strategyRank.getStockName());
            t.setDelFlag(StrategyRank.DELFLAG.NORMAL.code);
        })) > 0) {
            throw new BussinessException("股票名称或股票代码已存在");
        }
        //获取股票信息
        ModelRealTime modelRealTime = QuotationEndpoint.realtime(strategyRank.getStockCode());
        if(null == modelRealTime){
            throw new BussinessException("股票不存在");
        }
        strategyRank.setStatus(StrategyRank.STATUS.T1.code);
        strategyRank.setCreator(AdminSessionHelper.getCurrAdmin().getName());
        strategyRankService.insert(strategyRank);
        return ok();
    }


    /**
     * 编辑
     *
     * @param strategyRank
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("strategyRank/json/edit")
    @RequiresPermissions("sys:rank:edit")
    public ResultEntity editJson(StrategyRank strategyRank) throws Exception {
        checkModel(strategyRank);
        StrategyRank strategyRank1 = checkPrimaryKey(strategyRank.getId());
        if (!strategyRank1.getStockCode().equals(strategyRank)) {
            if (strategyRankService.selectCount(new StrategyRank(t -> {
                t.setStockName(strategyRank.getStockName());
                t.setDelFlag(StrategyRank.DELFLAG.NORMAL.code);
            })) > 0 || strategyRankService.selectCount(new StrategyRank(t -> {
                t.setStockCode(strategyRank.getStockCode());
                t.setDelFlag(StrategyRank.DELFLAG.NORMAL.code);
            })) > 0) {
                throw new BussinessException("股票代码或股票名称已存在");
            }
            //获取股票信息
            ModelRealTime modelRealTime = QuotationEndpoint.realtime(strategyRank.getStockCode());
            if(null == modelRealTime){
                throw new BussinessException("股票不存在");
            }
            strategyRank.setUpdator(AdminSessionHelper.getCurrAdmin().getName());
            strategyRankService.updateByPrimaryKeySelective(strategyRank);
        }
        return ok();
    }


    /**
     * 发布
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "strategyRank/json/publish")
    @RequiresPermissions("sys:rank:publish")
    public ResultEntity publish(Integer id) throws Exception {
        StrategyRank strategyRank = checkPrimaryKey(id);
        if (strategyRank.getStatus() == StrategyRank.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        strategyRankService.updateByPrimaryKeySelective(new StrategyRank(a -> {
            a.setId(strategyRank.getId());
            a.setPublishTime(new Date());
            a.setStatus(StrategyRank.STATUS.T2.code);
        }));
        return ok();
    }

    /**
     * 撤回
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "strategyRank/json/back")
    @RequiresPermissions("sys:rank:back")
    public ResultEntity back(Integer id) throws Exception {
        StrategyRank strategyRank = checkPrimaryKey(id);
        if (strategyRank.getStatus() != StrategyRank.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        strategyRankService.updateByPrimaryKeySelective(new StrategyRank(a -> {
            a.setId(strategyRank.getId());
            a.setStatus(StrategyRank.STATUS.T1.code);
        }));
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "strategyRank/json/del")
    @RequiresPermissions("sys:rank:del")
    public ResultEntity del(Integer id) throws Exception {
        StrategyRank strategyRank = checkPrimaryKey(id);
        if (strategyRank.getStatus() == StrategyRank.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        strategyRankService.updateByPrimaryKeySelective(new StrategyRank(a -> {
            a.setId(strategyRank.getId());
            a.setDelFlag(StrategyRank.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private StrategyRank checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            StrategyRank strategyRank = strategyRankService.selectOne(new StrategyRank(t -> {
                t.setId(id);
                t.setDelFlag(StrategyRank.DELFLAG.NORMAL.code);
            }));
            if (null != strategyRank) {
                return strategyRank;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }

    private void checkModel(StrategyRank strategyRank) {
        if (StringUtils.isBlank(strategyRank.getStockName())) {
            throw new BussinessException("股票名称不能为空");
        } else if (strategyRank.getStockName().length() > 30) {
            throw new BussinessException("股票名称长度不能超过30位");
        } else if (StringUtils.isBlank(strategyRank.getStockCode())) {
            throw new BussinessException("股票代码不能为空");
        } else if (strategyRank.getStockCode().length() > 30) {
            throw new BussinessException("股票代码长度不能超过30位");
        } else if (StringUtils.isBlank(strategyRank.getStockNum())) {
            throw new BussinessException("股票数量不能为空");
        } else if (!ValidateUtils.isPrice(strategyRank.getStockNum().toString())) {
            throw new BussinessException("股票数量格式不正确");
        } else if (StringUtils.isBlank(strategyRank.getStockType())) {
            throw new BussinessException("股票市场类型不能为空");
        } else if (strategyRank.getStockType() != StrategyRank.STOCKTYPE.T1.code && strategyRank.getStockType() != StrategyRank.STOCKTYPE.T2.code) {
            throw new BussinessException("股票市场类型格式有误");
        }
    }
}
